package com.liu.nb.security;

import com.liu.nb.entity.SysUser;
import com.liu.nb.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/*
@name:
@description:整个类是用户登录类，用来查找用户和用户对应的权限
@author: lzy
@title: 天王盖地虎!!!
@time:  2021-08-09 19:11:33
*/
@Component
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    ISysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByUsername(username);
        if (null==sysUser){
            throw new UsernameNotFoundException("用户名或密码不正确！");
        }
//        AccountUser是我继承了实现UserDetails接口的User类，然后添加了一些属性
        return new AccountUser(sysUser.getId(),sysUser.getUsername(),sysUser.getPassword(),getUserAuthority(sysUser.getId()));
    }
    /*
    * 获取用户权限信息(角色，菜单权限)
    *
    * */
    public List<GrantedAuthority> getUserAuthority(long userId){
        String  authority=sysUserService.getUserAuthorityInfo(userId);
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
    }
}
